package ui;

import entity.EntityContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

public class Menu extends JFrame {
    public Menu() {
        init();//创建对象时,调用页面初始化方法
    }
    /** 1.初始化界面组件和布局的 */
    private void init(){
        setTitle("指针信息在线测评");
        setSize(600, 400);
        //2.在初始化界面中添加创建内容窗格方法
        //add(createBtnPane());
        setContentPane(createContentPane());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        //setDefaultCloseOperation(3);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int op=JOptionPane.showConfirmDialog(null,"确定要退出程序？！");
                if (JOptionPane.YES_OPTION==op){
                    System.exit(0);
                }
            }
        });
    }

    /** 2.创建内容窗格*/
    private JPanel createContentPane() {
        JPanel pane = new JPanel(new BorderLayout());
        ImageIcon icon = new ImageIcon
                (this.getClass().getResource("pic/title.png"));
        pane.add(BorderLayout.NORTH, new JLabel(icon));
        pane.add(BorderLayout.CENTER, createMenuPane());
        pane.add(BorderLayout.SOUTH, new JLabel
                ("指针信息®©版权所有 盗版必究", JLabel.RIGHT));
        return pane;
    }

    /** 3.多个布局+图片*/
    private JLabel info;//info是私有成员变量
    private JPanel createMenuPane() {
        JPanel pane = new JPanel(new BorderLayout());
        //务必将info 引用到界面控件对象
        info = new JLabel("XXX 同学您好!", JLabel.CENTER);
        pane.add(BorderLayout.NORTH, info);
        pane.add(BorderLayout.CENTER, createBtnPane());
        return pane;
    }

    /*3.1 设置中部四个按钮,都是图片*/
    private JPanel createBtnPane() {
        JPanel pane = new JPanel();
        //这个createImgBtn是自定义的方法
        JButton start = createImgBtn("pic/exam.png", "开始");
        JButton result = createImgBtn("pic/result.png", "分数");
        JButton msg = createImgBtn("pic/message.png", "考试规则");
        JButton exit = createImgBtn("pic/exit.png", "离开");
        start.addActionListener(new MenuBtnAction());
        result.addActionListener(new MenuBtnAction());
        msg.addActionListener(new MenuBtnAction());
        exit.addActionListener(new MenuBtnAction());
        //将4个图片按钮添加到画板中
        pane.add(start);
        pane.add(result);
        pane.add(msg);
        pane.add(exit);
        return pane;
    }
    //创建图片按钮的方法
    private JButton createImgBtn(String img, String txt) {
        //获取图片
        ImageIcon ico = new ImageIcon(this.getClass().getResource(img));
        //创建按钮
        JButton button = new JButton(txt, ico);
        // 设置文本相对于图标的垂直位置(BOTTOM:底部)
        button.setVerticalTextPosition(JButton.BOTTOM);
        // 设置文本相对于图标的水平位置(CENTER:居中)
        button.setHorizontalTextPosition(JButton.CENTER);
        return button;
    }
    public void updateView(String name){
        info.setText(name+",欢迎登陆！");
    }
    /*public void loginError(){
        JOptionPane.showMessageDialog(null,"账号或密码错误！");

    }*/
    private ClientContext clientContext;

    public void setClientContext(ClientContext clientContext) {
        this.clientContext = clientContext;
    }
  /*  private EntityContext entiry;

    public void setEntiry(EntityContext entiry) {
        this.entiry = entiry;
    }*/

    private boolean examEnd=false;

    public void updateExamView() {
        this.examEnd=true;
    }

    class MenuBtnAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton btn = (JButton)e.getSource();
            String text = btn.getText();
            if (text.equals("开始")){
                if (!examEnd)
                {
                    try {
                        clientContext.start();
                    } catch (IOException | InterruptedException ioException) {
                        ioException.printStackTrace();
                    }
                }
                else {
                    JOptionPane.showMessageDialog(null,"您的该场考试已经结束！");
                }

            }
            else if (text.equals("分数")){
                System.out.println("分数");
                JOptionPane.showMessageDialog(null,"你的分数为："+clientContext.getScore()+"分");
            }
            else if (text.equals("考试规则")){
                    JOptionPane.showMessageDialog(null,clientContext.getRule());

            }
            else if (text.equals("离开")){
                int judge=JOptionPane.showConfirmDialog(null,"你确定要离开吗？");
                if (judge==JOptionPane.YES_OPTION){
                    clientContext.backLogin();
                }

                //System.exit(0);

            }
        }
    }
}
